package cn.wolfcode.web.controller;

import cn.wolfcode.domain.Department;
import cn.wolfcode.domain.Employee;
import cn.wolfcode.domain.Notice;
import cn.wolfcode.domain.Role;
import cn.wolfcode.qo.EmployeeQueryObject;
import cn.wolfcode.qo.JsonResult;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.IDepartmentService;
import cn.wolfcode.service.IEmployeeService;
import cn.wolfcode.service.INoticeService;
import cn.wolfcode.service.IRoleService;
import cn.wolfcode.util.RequiredPermission;
import cn.wolfcode.util.UserContext;
import com.github.pagehelper.PageInfo;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    private IEmployeeService employeeService;
    @Autowired
    private IDepartmentService departmentService;
    @Autowired
    private IRoleService roleService;
    @Autowired
    private INoticeService noticeService;
    //处理查询所有部门请求
    @RequestMapping("/list")
    @RequiredPermission(name = "员工列表",expression = "employee:list")
    public String list(Model model, @ModelAttribute("qo") EmployeeQueryObject qo){

        int num=noticeService.getUnreadNum(UserContext.getContextUser().getId());
        UserContext.setUnreadnumInSession(num);


        List<Notice> list = noticeService.getUnread(UserContext.getContextUser().getId());
        UserContext.setUnreadInSession(list);


        PageInfo<Employee> pageInfo = employeeService.query(qo);
        model.addAttribute("pageInfo",pageInfo);

        List<Department> departments = departmentService.listAll();
        model.addAttribute("departments",departments);
        return "employee/list";//找视图  "/WEB-INF/views/dept/list.jsp"
    }

    @RequestMapping("/delete")
    @RequiredPermission(name = "员工删除",expression = "employee:delete")
    public String delete(Long id){
        if(id!=null){
            employeeService.delete(id);
        }
        return "redirect:/employee/list";  //重定向
    }
    //去新增或去修改页面
    @RequestMapping("/input")
    @RequiredPermission(name = "员工新增或修改",expression = "employee:saveOrUpdate")
    public String input(Long id,Model model){
        List<Department> departments = departmentService.listAll();
        model.addAttribute("departments",departments);

        List<Role> roles = roleService.listAll();
        model.addAttribute("roles",roles);



        if(id!=null){
            Employee employee = employeeService.get(id);
            model.addAttribute("employee",employee);

            List<Role> roles1 = roleService.selectByEmpId(id);
            model.addAttribute("roles1",roles1);
        }
        return "employee/input";
    }
    @ResponseBody
    @RequestMapping("/saveOrUpdate")
    @RequiredPermission(name = "员工新增或修改",expression = "employee:saveOrUpdate")
    public JsonResult saveOrUpdate(Employee employee, Long[] ids){
            if(employee.getId()==null){
                employeeService.save(employee,ids);
            }else{
                employeeService.update(employee,ids);
            }
            return new JsonResult();


    }
    @RequiredPermission(name = "员工导出",expression = "employee:exportXls")
    @RequestMapping("/exportXls")
    public void exportXls(HttpServletResponse response) throws IOException {
        //指定文件名
        response.setHeader("Content-Disposition","attachment;filename=employee.xls");
        Workbook workbook = employeeService.exportXls();
        //把数据输出给浏览器
        workbook.write(response.getOutputStream());
    }

    @RequiredPermission(name = "员工导入",expression = "employee:importXls")
    @RequestMapping("/importXls")
    @ResponseBody
    public JsonResult importXls(MultipartFile file) throws IOException {
        employeeService.importXls(file);
        return new JsonResult();
    }
    @RequestMapping("/checkName")
    @ResponseBody
    public Map<String, Boolean> checkName(String username){
        HashMap<String,Boolean> map = new HashMap<>();
        Employee employee = employeeService.selectByUsername(username);
        map.put("valid",employee==null);
        return map;
    }

    @RequestMapping("/changePassword")
    public String changePassword(Long id){
            Employee employee = employeeService.get(id);
            employee.setPassword("1");
            employeeService.update(employee);
        return "redirect:/employee/list";



    }
}
